package com.zhou.framework.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.commons.collections.MapUtils;

import java.util.Map;

/**
 * @author lang.zhou
 * @since 2023/2/19
 */
public class MapQueryBuilder<T> {
    private LambdaQueryWrapper<T> w;
    private Map<String, Object> map;

    public MapQueryBuilder(LambdaQueryWrapper<T> w,Map<String, Object> map) {
        this.w = w;
        this.map = map;
    }

    public static <T> MapQueryBuilder<T> build(LambdaQueryWrapper<T> w, Map<String, Object> map){
        return new MapQueryBuilder<>(w, map);
    }

    public MapQueryBuilder<T> trimEq(SFunction<T, ?> f, String name){
        String value = MapUtils.getString(map,name,"").trim();
        if(!value.isEmpty()){
            w.eq(f,value);
        }
        return this;
    }
    public MapQueryBuilder<T> intEq(SFunction<T, ?> f, String name){
        int value = MapUtils.getIntValue(map,name,0);
        if(value > 0){
            w.eq(f,value);
        }
        return this;
    }
    public MapQueryBuilder<T> like(SFunction<T, ?> f, String name){
        String value = MapUtils.getString(map,name,"").trim();
        if(!value.isEmpty()){
            w.like(f,"%" + value + "%");
        }
        return this;
    }

    public MapQueryBuilder<T> eq(SFunction<T, ?> f, String name){
        w.eq(f,map.get(name));
        return this;
    }
}
